package com.guan.serviceStatistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guan.commonutils.R;
import com.guan.serviceStatistics.client.UcenterClient;
import com.guan.serviceStatistics.entity.StatisticsDaily;
import com.guan.serviceStatistics.mapper.StatisticsDailyMapper;
import com.guan.serviceStatistics.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author yaobai
 * @since 2023-04-25
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void createStatisticsByDay(String day) {

        //远程嗲用
        R registerR = ucenterClient.registerCount(day);
        int countRegister = (int)registerR.getData().get("countRegister");

        //把说查到的数据添加到数据库 统计分析表中
        StatisticsDaily sta = new StatisticsDaily();
        sta.setRegisterNum(countRegister);
        sta.setDateCalculated(day); //统计日期
        sta.setVideoViewNum(RandomUtils.nextInt(100,200));
        sta.setLoginNum(RandomUtils.nextInt(100,200));
        sta.setCourseNum(RandomUtils.nextInt(100,200));

        /*++ 统计表中实现一个日期一条记录，每次新注册人数，更新该条记录的register_num字段
        方法1. 先查询数库中是否存在当参数日期的数据，没有，就直接insert；有就update
        方法2. 在insert之前，先delete参数日期的记录，在insert
        */

        //先删除在添加
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);

        baseMapper.insert(sta);
    }

    /**
     * @date: 2023/4/25 22:52
     * @author: 管奇安
     * @description: 查询指定类型 指定条件的数据
     * @param:
     * [begin, 开始时间
     * end, 结束时间
     * type] 类型：注册人数 登录人数  视频播放数 。。。
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     **/
    @Override
    public Map<String, Object> getChartData(String begin, String end, String type) {
        QueryWrapper<StatisticsDaily> dayQueryWrapper = new QueryWrapper<>();
        dayQueryWrapper.select(type, "date_calculated");
        dayQueryWrapper.between("date_calculated", begin, end);

        List<StatisticsDaily> dayList = baseMapper.selectList(dayQueryWrapper);

        Map<String, Object> map = new HashMap<>();
        //前端要求json数组 对应java的list集合
        //数量
        List<Integer> dataList = new ArrayList<Integer>();
        //日期
        List<String> dateList = new ArrayList<String>();
        map.put("dataList", dataList);
        map.put("dateList", dateList);
        for (int i = 0; i < dayList.size(); i++) {
            StatisticsDaily daily = dayList.get(i);
            dateList.add(daily.getDateCalculated()); //获取日期 添加到集合
            switch (type) {
                case "register_num":
                    dataList.add(daily.getRegisterNum());
                    break;
                case "login_num":
                    dataList.add(daily.getLoginNum());
                    break;
                case "video_view_num":
                    dataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    dataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        return map;
    }
}
